package com.ah.shanxi_demo.sync.client;

import com.ah.shanxi_demo.common.enums.SXPortEnum;
import com.ah.http.httpsclient.MyHttpsClient;
import com.ah.http.httpsclient.constants.HttpHeaderContentTypeEnum;
import com.ah.shanxi_demo.sync.param.result.SyncReceiptResult;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

import static com.ah.shanxi_demo.sync.convert.ConvertSyncParam.convert_sync_receipt;
import static com.ah.shanxi_demo.sync.convert.ConvertSyncRequest.convert_sync_request_header;
import static com.ah.shanxi_demo.sync.utils.syncResultConvertObject.sync_result_order_receipt;

/**
 * 工单故障接口
 * @author zt 2020/8/26
 */
public class SyncReceiptClient {
    private static final Logger log = LoggerFactory.getLogger(SyncReceiptClient.class);

    /**
     * 部级平台 工单回执 客户端
     * @param syncReceiptResult
     * @return
     */
    public static Boolean receipt_client(SyncReceiptResult syncReceiptResult) {

        //生成工单接口地址
        String order_serviceAddress = syncReceiptResult.getService_address()+ SXPortEnum.WORK_ORDERS_RECEIPT.getKey();

        //调用部级平台  获取工单数据
        Object responseObject = sdk_receipt(syncReceiptResult, order_serviceAddress);

        //解析返回值 返回工单数据
        return sync_result_order_receipt(responseObject);
    }

    /**
     * 工单回执接口_SDK
     *
     * @param param
     * @param order_serviceAddress
     * @return
     */
    public static Object sdk_receipt(SyncReceiptResult param, String order_serviceAddress) {

        //地址
        String url = order_serviceAddress;

        //接口参数序列化
        Map<String, Object> paramsMap = convert_sync_receipt(param);

        //请求头
        Map<String, String> headerMap = convert_sync_request_header(paramsMap);

        try {

            log.info("sync_receipt【url】：" + JSON.toJSONString(url));
            log.info("sync_receipt【paramsMap】：" + JSON.toJSONString(paramsMap));
            log.info("sync_receipt【headerMap】：" + JSON.toJSONString(headerMap));

            Object responseObject = MyHttpsClient.sendPostHttps(url, paramsMap, headerMap, HttpHeaderContentTypeEnum.JSON.getKey());

            log.info("sync_receipt【responseObject】：" + JSON.toJSONString(responseObject));
            return responseObject;
        } catch (Exception e) {
            log.error("sync_receipt【Exception】：" + JSON.toJSONString(e));
        }
        return null;
    }

}
